var app = getApp();
const { $Toast } = require('../../dist/base/index');
var pageIndex, pageSize, hasNext;
var infoList = [];
var from,pages, currPage, prevPage;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    list: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    from=options.from;
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    pages = getCurrentPages();
    currPage = pages[pages.length - 1];   //当前页面
    prevPage = pages[pages.length - 2];  //上一个页面
    pageIndex = 1;
    pageSize = 10;
    infoList = [];
    this.getInfo()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if (hasNext) {
      pageIndex++;
      this.getInfo();
    } else {
      $Toast({
        content: '暂无更多',
        type: 'warning',
        duration: 2,
      });
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  getInfo() {
    $Toast({
      content: '请稍候',
      type: 'loading',
      duration: 0,
    });
    var that = this;
    wx.request({
      url: app.getUrl() + '/api/receive_info/receive_infos',
      data: {
        pageIndex: pageIndex,
        pageSize: pageSize,
        Authorization: app.globalData.customerId + ":" + app.globalData.accessToken,
      },
      method: 'POST',
      header: {
        "Content-Type": "application/x-www-form-urlencoded",
      },
      success(res) {
        $Toast.hide()
        if (res && res.statusCode == 200 && res.data && res.data.status == 200) {
          if (res.data.obj.total==0){
            $Toast({
              content: '暂无收货地址，赶紧添加一个吧',
              type: 'warning',
              duration: 3,
            });
          }
          hasNext = res.data.obj.hasNext;
          infoList = infoList.concat(res.data.obj.rows);
          that.setData({
            list: infoList,
          })
        } else {
          //token他处登录
          if (res.data.status == 403) {
            wx.showModal({
              title: '警告',
              content: '您的用户已在别的地方登录，是否重新登录？',
              success(res) {
                if (res.confirm) {
                  wx.navigateTo({
                    url: '../login/login',
                  })
                } else { }
              }
            })
          }
          // 没有登录状态
          else if (res.data.status == 406) {
            wx.switchTab({
              url: '../vipInfo/vipInfo',
              complete() {
                wx.showToast({
                  title: '您还没有登录，请登录后查看',
                  icon: "none",
                  duration: 3000
                })
              }
            })
          } else {
            $Toast({
              content: res.data.msg,
              type: 'warning',
              duration: 2,
            });
          }
        }
      },
      fail(res) {
        console.log(res)
        $Toast({
          content: '连接服务器失败',
          type: 'error',
          duration: 2,
        });
      }
    })
  },
  select(e){
    //如果是礼品兑换选择页过来的
    if(from){
      prevPage.setData({
        addressId:e.currentTarget.dataset.id,
      })
      wx.navigateBack({
        delta: 1
      })
    }else{
      
    }
  },
  add(){
    wx.navigateTo({
      url: '../addAddress/addAddress',
    })
  },
  edit(e){
    wx.navigateTo({
      url: '../addAddress/addAddress?id=' + e.currentTarget.dataset.id,
    })
  },
  delete(e){
    var that = this;
    wx.showModal({
      title: '提示',
      content: '确认要删除该地址吗？',
      success(res) {
        if (res.confirm) {
          wx.showLoading({
            title: '请稍候',
            mask: true
          })
          wx.request({
            url: app.getUrl() + '/api/receive_info/delete_receive_info',
            data: {
              infoId : e.currentTarget.dataset.id,
              Authorization: app.globalData.customerId + ":" + app.globalData.accessToken,
            },
            method: 'POST',
            header: {
              "Content-Type": "application/x-www-form-urlencoded",
            },
            success(res) {
              wx.hideLoading()
              if (res && res.statusCode == 200 && res.data && res.data.status == 200) {
                $Toast({
                  content: '删除成功',
                  type: 'success',
                  duration: 2,
                });
                that.onShow()
              } else {
                //token他处登录
                if (res.data.status == 403) {
                  wx.showModal({
                    title: '警告',
                    content: '您的用户已在别的地方登录，是否重新登录？',
                    success(res) {
                      if (res.confirm) {
                        wx.navigateTo({
                          url: '../login/login',
                        })
                      } else { }
                    }
                  })
                }
                // 没有登录状态
                else if (res.data.status == 406) {
                  wx.switchTab({
                    url: '../vipInfo/vipInfo',
                    complete() {
                      wx.showToast({
                        title: '您还没有登录，请登录后查看',
                        icon: "none",
                        duration: 3000
                      })
                    }
                  })
                } else {
                  $Toast({
                    content: res.data.msg,
                    type: 'warning',
                    duration: 2,
                  });
                }
              }
            },
            fail(res) {
              console.log(res)
              wx.hideLoading()
              $Toast({
                content: '连接服务器失败',
                type: 'error',
                duration: 2,
              });
            }
          })
        } else {}
      }
    })
  },
})